<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Model\Blog;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = DB::table('admin')->get();
        //dd($data);
        return view('admin.user',['user'=>$data]);
        
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.user_add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //自定义认证
      /* $this->validate($request, [
        'name'      => 'required|max:255',
        'phone'     => 'required|size:11',
        'password'  => 'required|confirmed|min:6',
        'email'     => 'required|email|max:255',
        ]);*/

        //验证成功后存入数据库
        $id = DB::table('admin')->insertGetId([
            'name'     => $request->name,
            'phone'    => $request->phone,
            'email'    => $request->email,
            'password' => bcrypt($request->password),
            ]);

        //dd($id);
        return redirect("/admin/user/$id");
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $data = DB::table('admin')->where('id',$id)->first();
        return view('admin.user_show',["val"=>$data]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $data = DB::table('admin')->where('id',$id)->first();
        return view('admin.user_edit',["user"=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        
     //自定义认证
      /* $this->validate($request, [
        'name'      => 'required|max:255',
        'phone'     => 'required|size:11',
        'password'  => 'required|confirmed|min:6',
        'email'     => 'required|email|max:255',
        ]);*/
       DB::table('admin')->update([
            'name'     => $request->name,
            'phone'    => $request->phone,
            'email'    => $request->email,
            'password' => bcrypt($request->password),
            ]);

        //dd($id);
        return redirect("/admin/user/$id");

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function image()
    {
        
        return view('admin.image');
    }
}
